{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "17b56dd1-61e0-4522-a508-abb0317e79a9",
   "metadata": {},
   "source": [
    "# Lesson 13: Zero-Shot Image Classification"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f6525c2",
   "metadata": {},
   "source": [
    "- In the classroom, the libraries are already installed for you.\n",
    "- If you would like to run this code on your own machine, you can install the following:\n",
    "\n",
    "```\n",
    "    !pip install transformers\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7a9fab6-3ab1-4445-82ea-3fd70268f195",
   "metadata": {},
   "source": [
    "- Load the model and the processor."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cb16fd2-2e18-4c0a-912b-c6cf737d8b45",
   "metadata": {},
   "source": [
    "- Here is some code that suppresses warning messages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eead1e9b-2273-4e90-b89e-a8bd997a7ba8",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "from transformers.utils import logging\n",
    "logging.set_verbosity_error()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09d30213-b2f4-4fb9-988e-98b1799e524e",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "from transformers import CLIPModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eaefb689-245e-4b20-833d-8cf197fa1eb2",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "model = CLIPModel.from_pretrained(\n",
    "    \"./models/openai/clip-vit-large-patch14\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45ce8a17-11f8-4a00-a6de-2f58f9cd020f",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "from transformers import AutoProcessor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7654301-8267-4cbd-a6df-1c220e258d32",
   "metadata": {
    "height": 47
   },
   "outputs": [],
   "source": [
    "processor = AutoProcessor.from_pretrained(\n",
    "    \"./models/openai/clip-vit-large-patch14\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f30a2210",
   "metadata": {},
   "source": [
    "More info about [openai/clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c780438c-f7d8-4c61-853a-695cb5ef0f16",
   "metadata": {},
   "source": [
    "- Load the image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "972ae61e-6c98-4384-9d73-da8a2550aaba",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18a1a89e-ef13-44d3-b048-7f2ce2624313",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "image = Image.open(\"./kittens.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11bcfa82-5d72-4883-b3fa-9d3fd101174b",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433ffd0b-916b-41ec-a228-094c3bae6d1a",
   "metadata": {},
   "source": [
    "- Set the list of labels from which you want the model to classify the image (above)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64fd646c-86a5-49bd-993a-0bd6293aa583",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "labels = [\"a photo of a cat\", \"a photo of a dog\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47bce093-ff57-421d-b5e0-a261d250f280",
   "metadata": {
    "height": 81
   },
   "outputs": [],
   "source": [
    "inputs = processor(text=labels,\n",
    "                   images=image,\n",
    "                   return_tensors=\"pt\",\n",
    "                   padding=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b69f984-34e7-42f3-8f7d-137bf87eaebf",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "outputs = model(**inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fff2a203-d24d-4bfa-8dfd-f948976bd756",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0b8b1fd-f6bf-4af4-99a7-18b493dc6b97",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "outputs.logits_per_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64bfc489-8d05-4405-bf84-16f0a1d41780",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "probs = outputs.logits_per_image.softmax(dim=1)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5a6f52d-ae9b-4e14-8a01-828ff789b8d1",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "probs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5e9aed6-098c-424f-9818-ba497aa56586",
   "metadata": {
    "height": 64
   },
   "outputs": [],
   "source": [
    "probs = list(probs)\n",
    "for i in range(len(labels)):\n",
    "  print(f\"label: {labels[i]} - probability of {probs[i].item():.4f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d00ce3e4",
   "metadata": {},
   "source": [
    "### Try it yourself! \n",
    "- Try this model with your own images and labels!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96a8d7a2-5a74-4076-a4a5-64580a6508fb",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
